﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace ClientServer.Utils
{
    public class GISUtils
    {

        /// <summary>
        ///  计算坐标距离GPS
        /// </summary>
        /// <param name="lngA">坐标A经度</param>
        /// <param name="latA">坐标A纬度</param>
        /// <param name="lngB">坐标B经度</param>
        /// <param name="latB">坐标B纬度</param>
        /// <returns></returns>
        public static  double AccountDistance(double lngA, double latA, double lngB, double latB)
        {
            double DistanceLng = 102834.74258026089786013677476285;
            double DistanceLat = 111712.69150641055729984301412873;
            double LngAbs = Math.Abs((lngA - lngB) * DistanceLng);
            double LatAbs = Math.Abs((latA - latB) * DistanceLat);
            return Math.Sqrt((LatAbs * LatAbs + LngAbs * LngAbs));
        }
        public static float CalcDistance(double lngA, double latA, double lngB, double latB)
        {
            double d1 = 0.01745329251994329;
            double d2 = lngA;
            double d3 = latA;
            double d4 = lngB;
            double d5 = latB;
            d2 *= d1;
            d3 *= d1;
            d4 *= d1;
            d5 *= d1;
            var d6 = Math.Sin(d2);
            var d7 = Math.Sin(d3);
            var d8 = Math.Cos(d2);
            var d9 = Math.Cos(d3);
            var d10 = Math.Sin(d4);
            var d11 = Math.Sin(d5);
            var d12 = Math.Cos(d4);
            var d13 = Math.Cos(d5);
            double[] arrayOfDouble1 = new double[3];
            double[] arrayOfDouble2 = new double[3];
            arrayOfDouble1[0]=d9 * d8;
            arrayOfDouble1[1]=(d9 * d6);
            arrayOfDouble1[2]=d7;
            arrayOfDouble2[0]=d13 * d12;
            arrayOfDouble2[1]=d13 * d10;
            arrayOfDouble2[2]=d11;
            var d14 = Math.Sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0]) +
                (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1]) +
                (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2]));

            return (float)(Math.Asin(d14 / 2.0) * 12742001.579854401);
        }


    }
}